package com.xuetang9.t9.hotel.mapper.sql.impl;

import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.condition.impl.BlackListCondition;
import com.xuetang9.t9.hotel.mapper.sql.IMapperSql;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * @author Pang
 */
public class BlackListMapperSql implements IMapperSql{
    @Override
    public String selectByAll(Map map) {
        ICondition condition = (ICondition) map.get("key");
        return getCondition(condition,"*",true);
    }
    @Override
    public String selectByCount(Map map) {
        ICondition key = (ICondition) map.get("key");
        return getCondition(key,"COUNT(*)",false);
    }


    private String getCondition(ICondition condition,String select,boolean isPage) {
        SQL sql = new SQL();
        sql.SELECT(select).FROM("g_blacklist");
        if(null == condition){
            return sql.toString();
        }
        BlackListCondition key = null;
        if(condition instanceof BlackListCondition){
            key = (BlackListCondition)condition;
        }else{
            throw new RuntimeException("condition type Don't match");
        }
        if(null != key){
            if(null != key.getName() && !key.getName().isEmpty()){
                sql.WHERE("guest_id IN (SELECT guest_id FROM g_guest WHERE guest_name LIKE '%"+key.getName()+"%')");
            }
            if(null != key.getNum() && !key.getNum().isEmpty()){
                sql.WHERE("guest_id IN ((SELECT guest_id FROM g_guest WHERE card_num LIKE '%"+key.getNum()+"%'))");
            }
            if(null != key.getGuestId() && key.getGuestId() <= 0){
                sql.WHERE("guest_id = "+key.getGuestId());
            }
            if(key.judge() && isPage){
                sql.LIMIT(key.getCurrPage()+","+key.getPageSize());
            }
        }
        return sql.toString();
    }

}
